package com.designpatternspart2.behavioraldesignpatterns.iteratorpattern;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Lance
 */
public class Menu
{

    private List<Item> menuItems;

    public Menu()
    {
        menuItems = new ArrayList<Item>();
    }

    public void addItem(Item item)
    {
        getMenuItems().add(item);
    }

    public Iterator<Item> iterator()
    {
        return new MenuIterator();
    }

    public List<Item> getMenuItems()
    {
        return menuItems;
    }

    public void setMenuItems(List<Item> menuItems)
    {
        this.menuItems = menuItems;
    }

    class MenuIterator implements Iterator<Item>
    {

        int currentIndex = 0;

        @Override
        public boolean hasNext()
        {
            if (currentIndex >= getMenuItems().size())
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        @Override
        public Item next()
        {
            return getMenuItems().get(currentIndex++);
        }

        @Override
        public void remove()
        {
            getMenuItems().remove(--currentIndex);
        }
    }
}
